#include "qmyProdSmetka.h"
#include "qizvodvnes.h"
#include "qizvodkorekcija.h"


QMyProdSmetka::QMyProdSmetka(QWidget *parent)
: QMyBaseWidget(parent)
,ProdSmetkaVnes(0)
,ProdSmetkaKorekcija(0)
,ProdSmetkaLista(0)
,m_row(0)
{
	ui.setupUi(this);
	pressF4();
}

QMyProdSmetka::~QMyProdSmetka()
{

}

void QMyProdSmetka::refresh()
{
}
void QMyProdSmetka::pressF2()
{
	closeAllForm();
	ProdSmetkaVnes = showMyWidget<QProdSmetkaVnes>(ProdSmetkaVnes); 
    ProdSmetkaVnes->setCategoryWidget(this);
	connect(ProdSmetkaVnes,SIGNAL(closeW()),this,SLOT(closeW()));
	connect(ProdSmetkaVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
	connect(ProdSmetkaVnes,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(ProdSmetkaVnes,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(ProdSmetkaVnes,SIGNAL(scallProProdSmetka(QWidget*, QString)),this,SLOT(callProProdSmetka(QWidget*, QString)));

	emit disablePrint(true);
}
void QMyProdSmetka::pressF3()
{
    if(!ProdSmetkaLista)
    {
        return;
    }
    int id = ProdSmetkaLista->getSelectedId();
    closeAllForm();
    ProdSmetkaKorekcija = showMyWidget<QProdSmetkaKorekcija>(ProdSmetkaKorekcija); 
    ProdSmetkaKorekcija->setCategoryWidget(this);
    connect(ProdSmetkaKorekcija,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(ProdSmetkaKorekcija,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(ProdSmetkaKorekcija,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	connect(ProdSmetkaKorekcija,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(ProdSmetkaKorekcija,SIGNAL(scallProProdSmetka(QWidget*, QString)),this,SLOT(callProProdSmetka(QWidget*, QString)));
    ProdSmetkaKorekcija->init(id);
	emit disablePrint(true);
}
void QMyProdSmetka::pressF4()
{
    closeAllForm();
    ProdSmetkaLista = showMyWidget<QProdSmetkaLista>(ProdSmetkaLista); 
    ProdSmetkaLista->setCategoryWidget(this);
	ProdSmetkaLista->setRow(m_row);
    connect(ProdSmetkaLista, SIGNAL(closeW()), this, SLOT(closeWLista()));
    connect(ProdSmetkaLista, SIGNAL(procF2()), this, SLOT(procF2()));
    connect(ProdSmetkaLista, SIGNAL(procF3()), this, SLOT(procF3()));
    connect(ProdSmetkaLista, SIGNAL(procDel()), this, SLOT(procDel()));
	connect(ProdSmetkaLista, SIGNAL(sProdSmetkaToIzvod(QString&, QString&, QString&, QString&)), this, SLOT(retProdSmetkaToIzvod(QString&, QString&, QString&, QString&)));
	emit disablePrint(false);

}
void QMyProdSmetka::procF2()
{
    pressF2();
}
void QMyProdSmetka::procF3()
{
    pressF3();
}
void QMyProdSmetka::procDel()
{

}
void QMyProdSmetka::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);

    emit signCloseMyWidget();
}
void QMyProdSmetka::closeW()
{
    pressF4();
}
void QMyProdSmetka::closeWLista()
{
    pressEscape();
}

void QMyProdSmetka::closeAllForm()
{
    ProdSmetkaKorekcija = deleteMyWidget<QProdSmetkaKorekcija>(ProdSmetkaKorekcija);
    ProdSmetkaVnes = deleteMyWidget<QProdSmetkaVnes>(ProdSmetkaVnes);
	if (ProdSmetkaLista)
	{
		m_row = ProdSmetkaLista->getRow();
	}
    ProdSmetkaLista = deleteMyWidget<QProdSmetkaLista>(ProdSmetkaLista);
}
void QMyProdSmetka::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}

void QMyProdSmetka::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}

void QMyProdSmetka::callIspratnici(QWidget* myobj, QString text)
{
	emit signImportArtikliFromIspratnici(myobj, text);
}

void QMyProdSmetka::callProProdSmetka(QWidget* myobj, QString text)
{
	emit signImportArtikliFromProProdSmetka(myobj, text);
}

void QMyProdSmetka::sendPrintMessage()
{
    emit signPrint();
}

QStandardItemModel* QMyProdSmetka::getModel()
{
    QStandardItemModel * modelExt;
    if (ProdSmetkaLista)
    {
        modelExt = ProdSmetkaLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyProdSmetka::getModel2()
{
    QStandardItemModel * modelExt;
    if (ProdSmetkaLista)
    {
        modelExt = ProdSmetkaLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyProdSmetka::getIspratnicaInfo()
{
    QStringList modelExt;
    modelExt.clear();

    if (ProdSmetkaLista)
    {
        modelExt = ProdSmetkaLista->geInfo();
    } 
    else
    {
        modelExt;
    }
    return modelExt;
}

void QMyProdSmetka::retProdSmetkaToIzvod(QString& text6, QString& text8, QString& text9, QString& textName)
{
	QWidget *sour = getSourceWidget();
	if (qobject_cast<QIzvodVnes*>(sour))
	{
		QIzvodVnes *dest = qobject_cast<QIzvodVnes*>(sour);
//		dest->setProdSmetka(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QIzvodKorekcija*>(sour))
	{
		QIzvodKorekcija *dest = qobject_cast<QIzvodKorekcija*>(sour);
//		dest->setProdSmetka(text6, text8, text9, textName);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
}

void QMyProdSmetka::setInitSearchText(QString text)
{
    if (ProdSmetkaLista)
    {
        ProdSmetkaLista->setInitText(text);
    } 
}